package com.cbs.utils;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import com.cbs.log.L;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by brochexu on 2/13/14.
 */
public final class S {
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public static String getDeviceId(Context context) {
        android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String device_id = tm.getDeviceId();

        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        String mac = wifi.getConnectionInfo().getMacAddress();

        if (TextUtils.isEmpty(device_id)) {
            device_id = mac;
        }

        if (TextUtils.isEmpty(device_id)) {
            device_id = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
        }

        return device_id;
    }

    public static String getVersionName(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            L.e(S.class.getName(), e.getMessage());
            return "";
        }
    }

    public static int getVersionCode(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            L.e(S.class.getName(), e.getMessage());
            return 0;
        }
    }

    public static double getTimeStamp() {
        return java.lang.System.currentTimeMillis() / 1000.0;
    }

    public static String getDeviceInfo(Context context) {
        try {
            JSONObject json = new JSONObject();

            android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String mac = wifi.getConnectionInfo().getMacAddress();
            json.put("mac", mac);

            String device_id = getDeviceId(context);
            json.put("device_id", device_id);

            return json.toString();
        } catch (Exception e) {
            L.e(S.class.getName(), e.getMessage());
        }
        return null;
    }

    //    Toast.makeText(MainActivity.this, S.getSystemProperty("ro.miui.ui.version.name"), 3000).show();
    public static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            L.e(S.class.getName(), ex.getMessage());
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    L.e(S.class.getName(), e.getMessage());
                }
            }
        }
        return line;
    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return 进程号
     */
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void copyToClipboard(Context context, String content) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("result", content);
        clipboardManager.setPrimaryClip(clipData);
    }
}
